//
//  MusicViewController.m
//  CarNetwork
//
//  Created by fzuacm on 16/4/18.
//  Copyright © 2016年 fzu_z. All rights reserved.
//

#import "MusicViewController.h"
#import "MusicTableViewCell.h"
#import <AVFoundation/AVFoundation.h>

@interface MusicViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_TableView;
    UIImageView *_imageView;
    UIButton *_playButton;
    NSURL *musicUrl;
}
@property(nonatomic,strong)AVAudioPlayer *_player;

@end

@implementation MusicViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //self.title = @"音乐";
    [self creatTableView];
    [self creatView];
    [self createMusic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma view

-(void)creatView
{
    _playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _playButton.frame=CGRectMake(140, 400, 40, 40);
    [_playButton setBackgroundColor:[UIColor grayColor]];
    _playButton.layer.masksToBounds=YES;
    _playButton.layer.cornerRadius=20;
    [_playButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"musicpause.png"]] forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(selected:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_playButton];
}

-(void)selected:(UIButton *)btn
{
    if (btn.backgroundColor == [UIColor whiteColor]) {
        [self._player play];
        NSLog(@"white");
        [btn setBackgroundColor:[UIColor grayColor]];
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"musicpause.png"]] forState:UIControlStateNormal];
    }
    else
    {
        [self._player pause];
        NSLog(@"gre");
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"musicplay.png"]] forState:UIControlStateNormal];
    }
}

-(void)creatTableView
{
    _TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _TableView.dataSource = self;
    _TableView.delegate = self;
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"default.png"]]];
    
    _TableView.backgroundColor = [UIColor clearColor];
    [_TableView setBackgroundView:_imageView];
    [self.view addSubview:_TableView];
}

-(void)createMusic
{
    if (self._player==nil) {
        if (musicUrl==nil) {
            musicUrl=[NSURL alloc];
        }
        musicUrl=[[NSBundle mainBundle] URLForResource:@"viva la viva.mp3" withExtension:nil];
        
        self._player = [[AVAudioPlayer alloc] initWithContentsOfURL:musicUrl error:nil];
        
        [self._player prepareToPlay];
        
        [self._player play];
    }

}


#pragma delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"MusicTableViewCell";
    MusicTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.backgroundColor = nil;
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellID owner:self options:nil] lastObject];
        cell.MusicLabel.textColor = [UIColor blackColor];
        switch (indexPath.row+1) {
            case 1:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.Viva la viva",indexPath.row+1];
                break;
            case 2:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.Unity",indexPath.row+1];
                break;
            case 3:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.Fade",indexPath.row+1];
                break;
            case 4:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.Every breath you take",indexPath.row+1];
                break;
            case 5:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.Hotel California",indexPath.row+1];
                break;
            case 6:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.thank you",indexPath.row+1];
                break;
            case 7:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.身未动，心已远-群星",indexPath.row+1];
                break;
            case 8:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.风筝-陈升",indexPath.row+1];
                break;
            case 9:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.Colorful days",indexPath.row+1];
                break;
            case 10:
                cell.MusicLabel.text = [NSString stringWithFormat:@"%d.奔跑-黄征",indexPath.row+1];
                break;

            
                
            default:
                break;
        }
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.MusicLabel.textColor = [UIColor orangeColor];
    [self._player pause];
    
    switch (indexPath.row+1) {
        case 1:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"viva la viva.mp3" withExtension:nil];
        }
            break;
        case 2:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"unity.mp3" withExtension:nil];
        }
            break;
        case 3:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"fade.mp3" withExtension:nil];
        }
            break;
        case 4:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"every breath you take.mp3" withExtension:nil];
        }
            break;
        case 5:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"Hotel California.mp3" withExtension:nil];
        }
            break;
        case 6:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"thank you.mp3" withExtension:nil];
        }
            break;
        case 7:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"身未动，心已远-群星.mp3" withExtension:nil];
        }
            break;
        case 8:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"风筝-陈升.mp3" withExtension:nil];
        }
            break;
        case 9:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"Colorful days.mp3" withExtension:nil];
        }
            break;
        case 10:
        {
            musicUrl=[[NSBundle mainBundle] URLForResource:@"奔跑-黄征.mp3" withExtension:nil];
        }
            break;
        default:
            break;
    }
    self._player =[[AVAudioPlayer alloc] initWithContentsOfURL:musicUrl error:nil];
    [self._player prepareToPlay];
    [self._player play];
    
    [_playButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"musicpause.png"]] forState:UIControlStateNormal];
    [_playButton setBackgroundColor:[UIColor grayColor]];
    [_playButton addTarget:self action:@selector(selected:) forControlEvents:UIControlEventTouchUpInside];
    //播放音乐
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.MusicLabel.textColor = [UIColor blackColor];
    //停止播放音乐
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
